<template>
  <!-- 参加培训情况 -->
  <div class="app-container">
    <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
            v-hasPermi="['clr:training:applyTraining']"
          >申请</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clr:batch:edit']"
        >修改</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['clr:batch:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table :data="blackList">
      <el-table-column
        label="学院"
        align="center"
        prop="collegeName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="clrName">
      </el-table-column>
      <el-table-column label="职称" align="center" prop="studentName">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.basic_title_type"
            :value="scope.row.title"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="身份证号"
        align="center"
        prop="cardCode"
      ></el-table-column>
      <el-table-column
        label="培训项目名称"
        align="center"
        prop="trainingProgram"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="参加培训时间"
        align="center"
        prop="trainingDate"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="学时"
        align="center"
        prop="trainingHours"
        show-overflow-tooltip
        width="100"
      >
      </el-table-column>
      <el-table-column label="证书编号" align="center" prop="certNumber" show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        label="发证机构名称"
        align="center"
        prop="issuingAuthority"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="附件" align="center" prop="attachment">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleExport(scope.row)" v-if="scope.row.attachment"
            >下载附件</el-button
          >
          <el-button  size="mini" type="text" v-if="!scope.row.attachment"
            >暂无附件</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" @click="handleEdit(scope.row)"   v-hasPermi="['clr:training:query']"
            >修改</el-button
          > -->
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"   v-hasPermi="['clr:training:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 申请 -->
    <el-dialog
      title="申请"
      :visible.sync="open"
      width="60%"
      append-to-body
      @close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rulesName" label-width="120px">
        <el-row>
        
          <el-col :span="12">
            <el-form-item label="培训项目名称" prop="trainingProgram">
              <el-input
                v-model="form.trainingProgram"
                placeholder="请输入培训项目名称"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="参加培训时间" prop="trainingDate">
              <el-date-picker
                v-model="form.trainingDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择时间"
              >
              </el-date-picker>
              <!-- <yearTime @input="timeDate" :value="amcYear"></yearTime> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学时" prop="trainingHours">
              <el-input v-model="form.trainingHours" placeholder="请输入学时" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证书编号" prop="certNumber">
              <el-input
                v-model="form.certNumber"
                placeholder="请输入证书编号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发证机构名称" prop="issuingAuthority">
              <el-input
                v-model="form.issuingAuthority"
                placeholder="请输入发证机构名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件" prop="attachment">
              <uploud @input="changeLoud"></uploud>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
      <script>
import { collegeSelect } from "@/api/information/studentInfor";
import {downloadGet} from '@/utils/request'

import {
  trainingList,
  trainingAdd,
  trainingUpdata,
  getTraining,
  trainingDel,
} from "@/api/clr/trainList";
import searchForm from "@/components/Search/searchForm.vue";
import { getDicts } from "@/api/system/dict/type";
import uploud from "./components/uploud.vue";
import Validators from "@/utils/validate.js";
export default {
  name: "train",
  components: { searchForm, uploud },
  dicts: [
    "dep_grad_scope",
    "dep_grad_status",
    "basic_title_type", // 职称类型
  ],

  data() {
    return {
      formLabelWidth: "100px",
      formOptions: [
        {
          label: "学院",
          prop: "deptId",
          element: "el-select",
          options: [],
          placeholder: "请选择",
        },
        {
          label: "姓名",
          prop: "clrName",
          element: "el-input",
          placeholder: "请输入姓名",
        },
        {
          label: "职称",
          prop: "title",
          element: "el-select",
          options: [],
          placeholder: "请选择",
        },
        {
          label: "身份证号",
          prop: "cardCode",
          element: "el-input",
          placeholder: "请输入身份证号",
        },
        {
          label: "培训项目名称",
          prop: "trainingProgram",
          element: "el-input",
          placeholder: "请输入培训项目名称",
        },
        {
          label: "参加培训时间",
          prop: "applyTime",
          type: "daterange",
          element: "el-date-picker",
          placeholder: "请选择时间"
        },
        {
          label: "学时",
          prop: "trainingHours",
          element: "el-input",
          placeholder: "请输入学时",
        },
        {
          label: "证书编号",
          prop: "certNumber",
          element: "el-input",
          placeholder: "请输入证书编号",
        },
        {
          label: "发证机构名称",
          prop: "issuingAuthority",
          element: "el-input",
          placeholder: "请输入发证机构名称",
        },
      ],
      // 字典表格数据
      blackList: [],
      total: 0,
      form: {},
      title: "",
      // 显示搜索条件
      showSearch: true,
      open: false, // 弹窗开关
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      rulesName: {
        // collegeName: [
        //   { required: true, message: "请选择所属院系", trigger: "change" },
        // ],
        // clrName: [
        //   {
        //     required: true,
        //     text: "请输入姓名",
        //     validator: Validators.isString,
        //     trigger: "blur",
        //   },
        // ],
        // title: [{ required: true, message: "请选择职称", trigger: "change" }],
        // cardCode: [
        //   {
        //     required: false,
        //     text: "请输入身份证",
        //     validator: Validators.idNumber,
        //     trigger: "blur",
        //   },
        // ],
        trainingProgram: [
          {
            required: true,
            text: "请输入培训项目名称",
            validator: Validators.isStr,
            trigger: "blur",
          },
        ],

        trainingDate: [
          { required: true, message: "请选择培训时间", trigger: "change" },
        ],
        trainingHours: [
          {
            required: true,
            message: "请输入学时",
            trigger: "blur",
          },
        ],
        certNumber: [
          {
            required: true,
            message: "请输入证书编号",
            trigger: "blur",
          },
        ],
        issuingAuthority: [
          {
            required: true,
            text: "请输入发证机构名称",
            validator: Validators.isStr,
            trigger: "blur",
          }, 
        ],
      },
      deptOptions: [], // 院系数据 
      dateRange:[] // 时间范围
    };
  },
  created() {
    getDicts("basic_title_type").then((res) => {
      this.formOptions[2].options = res.data.map((item) => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"],
        };
      });
    });
    this.department();
    this.getList();
  },
  mounted() {},
  methods: {
    // 获取搜索表单提交的数据
    onSearch(val) {
      if(val.applyTime){
      this.dateRange = val.applyTime
        }
      this.queryParams = Object.assign((this.queryParams, val));
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    onReset(data) {
      data.params = {}
      this.dateRange = []
      this.queryParams.pageNum = 1;
      this.queryParams = Object.assign(this.queryParams, data);
      this.getList();
    },
    getList() {
      trainingList(this.addDateRange(this.queryParams,this.dateRange)).then((response) => {
        console.log(response, "response");
        this.blackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 修改
    handleEdit(row) {
      getTraining(row.catId).then((res) => {
        console.log(res, "详情");
        this.form = res.data;
        this.title = "修改";
        this.open = true;
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除"' + row.clrName + '"的参加培训的数据项？')
        .then(function () {
          return trainingDel(row.catId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.clrExamineStaffList = this.clrExamineStaffList;
          if (this.form.catId != null) {
            trainingUpdata(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            trainingAdd(this.form).then((response) => {
              this.$modal.msgSuccess("申请成功");
              this.open = false;
              this.getList();
            });
          }
          this.$refs.uploadList.uploadMethod();
        }
      });
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    // 获取部门信息
    async department() {
      await collegeSelect().then((res) => {
        this.deptOptions = res.data;
        if (this.formOptions && this.formOptions.length > 0) {
          this.formOptions[0].options = res.data.map((item) => {
            return {
              value: item["deptId"],
              label: item["deptName"],
            };
          });
        }
      });
    },
    // 上传
    changeLoud(data) {
      console.log(data, "数据");
      this.form.attachment = data;
    },
   
    // 下载附件
    // handleExport(row) {
    //     if (row.attachment) {
    //       downloadGet(
    //         "/common/download",
    //         {
    //           params:{
    //             fileName: row.attachment
    //           }
    //         },
    //       );        } else {
    //       this.$modal.msgWarning("未找到上传结果");
    //     }
    //    },
    handleExport(row) {
      let link = document.createElement("a");
      //创建一个a标签
      link.style.display = "none";
      //将a标签隐藏
      link.href = process.env.VUE_APP_BASE_API + row.attachment;
      //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
      link.setAttribute("download", row.clrName + "参加培训情况");
      // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
      document.body.appendChild(link);
      //将上面创建的a标签加入到body的尾部
      link.click();
      //执行a标签
    },
    // 新增
    handleAdd() {
      this.open = true;
      this.title = "申请";
    },
  },
};
</script>